#lang racket

;;racket内置filter
(define (filter prediate sequence)
  (cond ((null? sequence) '())
        ((prediate (car sequence))
         (cons (car sequence)
               (filter prediate (cdr sequence))))
        (else
         (filter prediate (cdr sequence)))))

;;自己实现的filter,支持多重列表
(define (filter2 prediate sequence)
  (cond ((null? sequence) '())
        ((list? (car sequence))
         (cons (filter2 prediate (car sequence))
               (filter2 prediate (cdr sequence))))
        ((prediate (car sequence))
                   (cons (car sequence)
                         (filter2 prediate (cdr sequence))))
        (else
         (filter2 prediate (cdr sequence)))))
(filter2 (lambda (x) (> x 1)) '(1 2 7 (4 5) 3 (4 5 6)))
